package com.huangliusong.quickstart;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author huangliusong
 * @since 2018/11/25
 * {@link }
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //创建一个连接工厂
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("/");
        //创建连接通过工厂
        Connection connection=connectionFactory.newConnection();
        //通过conection创建一个channel
        Channel channel=connection.createChannel();
        //创建一个队列
        channel.queueDeclare("test001",true,false,false,null);
        //创建一个消费者
        QueueingConsumer queueingConsumer=new QueueingConsumer(channel);
        //设置channel
        channel.basicConsume("test001",true,queueingConsumer);
        //获取消息
        while (true){
            QueueingConsumer.Delivery delivery=queueingConsumer.nextDelivery();
            String message=new String(delivery.getBody());
            System.out.println("消费端消费："+message);
            //Envelope envelope=delivery.getEnvelope();
            //envelope.getDeliveryTag();
        }
    }
}
